<?php

!defined('IN_VIFRAME') && die('Access Denied.');

class OSystemMessage extends TObject {

	protected
		$_Title   = '';

	protected
		$_Content = '';
		
	protected
		$_Style   = '';

	public function __construct($title, $content = '') {
		$this->_Style  = Config::get('Message.System.Style');
		
		$this->_Title   = $title;
		$this->_Content = $content;
	}

	public function getTitle() {
		return $this->_Title;
	}
		
	public function setTitle($title) {
		$this->_Title = $title;
		
		return $this;
	}
	
	public function getContent() {
		return $this->_Content;
	}
	
	public function setContent($content) {
		$this->_Content = $content;
		
		return $this;
	}
	
	public function getStyle() {
		return $this->_Style;
	}
	
	public function setStyle($key, $value = null) {
		if (is_arraay($key)) {
			foreach ($key as $cntKey => $cntValue) {
				$this->_Style .= "$cntKey:$cntValue;";
			}
		} elseif (is_null($value)) {
			$this->_Style = $key;
		} else {
			$this->_Style .= "$key:$value;";
		}
		
		return $this;
	}
	
	public function appendSubtitle($subtitle, $tab = 0) {
		$tab     = $this->_filterParam('Subtitle.Tab', $tab);
		
		$output  = '';
		$output .= str_repeat(' ', $tab*4);
		$output .= $subtitle."\n";
		
		$this->_Content .= $output;
		
		return $this;
	}
	
	public function appendProperty($key, $value = '', $minLength = false, $tab = false) {
		
		$minLength = $this->_filterParam('Property.MinLength', $minLength);
		$tab       = $this->_filterParam('Property.Tab', $tab);
		
		$output  = '';
		$output .= str_repeat(' ', $tab*4);
		$output .= $key;
		
		$space   = $minLength - strlen($key);
		if ($space < 0) $space = 0;
		
		$output .= str_repeat(' ', $space);
		$output .= ": $value\n";
		
		$this->_Content .= $output;
		
		return $this;
	}
	
	public function appendExport($key, $value,
			$minLength = false, $tab = false) {
			
		$minLength = $this->_filterParam('Export.MinLength', $minLength);
		$tab       = $this->_filterParam('Export.Tab', $tab);
		
		$space  = str_repeat(' ', $tab * 4 + $minLength + 2);
		
		$export = var_export($value, true);
		$export = stripslashes($export);
		
		$export = str_replace('  ', '    ', $export); // Indent.
		$export = str_replace("\n", "\n$space", $export); // Add the space before each line.
		
		$export = str_replace('array (', '{', $export); // Change 'array (' to '{'.
		$export = preg_replace('/=>[\s]*/', '=> ', $export); // Format the space after '=>'.
		$export = preg_replace('/,([\s]*)\)/', '\\1}', $export); // Remove the last ',' in an array and change ')' to '}'.
		
		$export = preg_replace("/'(.+?)' =>/", '[\\1] =>', $export); // Format the key in an array.
		$export = preg_replace('/(\d+?) =>/', '[\\1] =>', $export); // Format the number key in an array.
		$export = str_replace('=>', ':', $export); // Change '=>' to ':'.
		
		return $this->appendProperty($key, $export, $minLength, $tab);
	}
	
	public function appendTrace($key, $trace,
			$minLength = false, $tab = false) {
		
		$minLength = $this->_filterParam('Trace.MinLength', $minLength);
		$tab       = $this->_filterParam('Trace.Tab', $tab);
		
		$this->appendProperty($key, '', $minLength, $tab)
		     ->appendLine();
		
		$traceCount = count($trace);

		if (0 === $traceCount) {
			$this->appendSubtitle('(None)', $tab + 1);
		} else {
			for ($i = $traceCount - 1;$i >= 0;--$i) {
				$cntTrace = $trace[$i];
				
				$this->appendSubtitle('Id - '.($traceCount - $i), $tab + 1)
				   	 ->appendProperty('File', $cntTrace['file'], 10, $tab + 2)
				     ->appendProperty('Line', $cntTrace['line']);
				     
				if (!isset($cntTrace['type'])) {
					$this->appendProperty('Type', 'Function')
					     ->appendProperty('Call', $cntTrace['function']);
				} elseif ('->' == $cntTrace['type']) {
					$this->appendProperty('Type', 'Method')
					     ->appendProperty('Call', $cntTrace['class'].'->'.$cntTrace['function']);
				} else {
					$this->appendProperty('Type', 'Static Method')
					     ->appendProperty('Call', $cntTrace['class'].'::'.$cntTrace['function']);
				}
				
				$this->appendProperty('Arguments', '');
				
				if (!empty($cntTrace['args'])) {
					foreach ($cntTrace['args'] as $id => $argument) {
						$this->appendExport("$id", $argument, 3, $tab + 3);
					}
				} else {
					$this->appendSubtitle('(None)', $tab + 3);
				}
				
				$this->appendLine();
			}
		}
		
		return $this;
	}
	
	public function appendSeparator($style = null) {
		if (empty($style)) $style = Config::get('Message.System.SeparatorStyle');
		
		$this->_Content .= '<hr style="'.$style.'" />';
		
		return $this;
	}
	
	public function appendString($string, $tab = false) {
		$tab             = $this->_filterParam('String.Tab', $tab);
		$this->_Content .= str_repeat(' ', $tab * 4).$string;
		
		return $this;
	}
	
	public function appendLine() {
		$this->_Content .= "\n";
		
		return $this;
	}
	
	public function flush($die = false) {
		$output  = '';
		$output .= "<pre style='{$this->_Style}'>\n";
		$output .= "<strong style='".Config::get('Message.System.TitleStyle')
				."'>{$this->_Title}</strong>\n\n";

		$output .= $this->_Content;
		
		$output .= Vi::getPowered()."\n";
		if ($die) $output .= Config::get('Message.System.DieMassage');
		$output .= "</pre>\n";

		echo($output);
		if ($die) die();
		
		return $this;
	}
}
